﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Logistics.DataManage.Local;
using MapgisEbp.QuickDataBase;
using System.Data;
using Logistics.DataManage.DAL;
using Logistics.Utils;

namespace Logistics.DataManage
{
    class SitesDAO
    {
        public static String GetSiteName(String Id)
        {
            String sql = "select SiteName from SiteInfo where SiteID=?";
            ParameterizedSQL PSQL = LocalDb.Handler.DbAdapter.AdaptSQLAnonymousParams(sql, Id);
            Object obj = LocalDb.Handler.ExecuteScalar(PSQL.SQL, PSQL.Parameters);
            return obj == null ? "" : obj.ToString();
        }

        public static OSites GetSite(String Id)
        {
            String sql = "select * from SiteInfo where SiteID=?";
            ParameterizedSQL PSQL = LocalDb.Handler.DbAdapter.AdaptSQLAnonymousParams(sql, Id);
            DataSet ds = LocalDb.Handler.ExecuteQuery(PSQL.SQL, PSQL.Parameters);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                OSites site = new OSites();
                DataRow row = ds.Tables[0].Rows[0];
                site.SiteID = Convert.ToString(row["SiteID"]);
                site.SiteName = Convert.ToString(row["SiteName"]);
                site.SiteAreaCode = Convert.ToString(row["SiteAreaCode"]);
                return site;
            }
            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static IDictionary<String, String> GetSites()
        {
            IDictionary<String, String> dict = new Dictionary<String, String>();
            String sql = "select * from SiteInfo";
            DataSet ds = LocalDb.Handler.ExecuteQuery(sql);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    dict.Add(Convert.ToString(row["SiteID"]), Convert.ToString(row["SiteName"]).Trim());
                }
            }
            return dict;
        }

        public static String GetSiteIdByName(String siteName)
        {
            String sql = "select SiteID from SiteInfo where trim(SiteName)=?";
            ParameterizedSQL PSQL = LocalDb.Handler.DbAdapter.AdaptSQLAnonymousParams(sql, siteName);
            Object obj = LocalDb.Handler.ExecuteScalar(PSQL.SQL, PSQL.Parameters);
            return obj == null ? "" : obj.ToString();
        }

        public static IDictionary<String, String> GetDesSitesByStartSite(String startSiteID)
        {
            string strSQL = "SELECT s.SiteName,s.SiteID  FROM Routes r left join SiteInfo s on r.EndSiteID=s.SiteID where r.BeginSiteID='" + startSiteID + "'";
            DataSet ds_endSite = LocalDb.ExecuteQuery(strSQL);
            var dictx = CollectionUtils.FillDictionary(ds_endSite.Tables[0], "SiteID", "SiteName", false);
            return dictx;
        }
    }
}
